#include "StdAfx.h"
#include "bayesclas.h"
#include <math.h>
CBayesClas::CBayesClas(void)
{
//	for(int i=0;i<4096;i++)
//	{
////		Hist[i] = new double [255];
////		if(NULL == Hist[i])
////			printf("No Memory!!!\n");
//		PosHist[i] = new double [255];
//		NegHist[i] = new double [255];
//	}
	m_nFeaSize=896;
	PosHist = new double [255*m_nFeaSize];
	NegHist = new double [255*m_nFeaSize];

	if(0)//load from file
	{
		char modelFile[256];
		char pModuleName[256];
		::GetModuleFileName(g_hModule,pModuleName,256);
		CString strTemp;
		strTemp=pModuleName;
		int pos=strTemp.ReverseFind('\\');
		CString strModuleFile=strTemp.Left(pos);
		strcpy(modelFile,strModuleFile+"\\Elbp896Bayes.MDL");

	//	AfxMessageBox(modelFile);
		loadModel(modelFile);
	}
	else//load from resource
	{
		loadModelFromRes("IDR_ELBP896_BAYES");
	}
}

CBayesClas::~CBayesClas(void)
{
//	for(int i=0;i<4096;i++)
//	{
////		delete Hist[i];
//		delete PosHist[i];
//		delete NegHist[i];
//	}
	delete []PosHist;
	delete []NegHist;
}

int CBayesClas::loadModel(char filename[])
{
/*	int i,j;
	FILE * f = fopen(filename,"rt");
	double temp;
	if(NULL == f)
		return 0;
	for(i=0;i<4096;i++)
	{
		for(j=0;j<255;j++)
		{
			fscanf(f,"%lf",&temp);
			Hist[i][j]=temp;
		}
	}
	fclose(f);
	return 1;*/
	int i,j;
	FILE * f = fopen(filename,"rt");
	double temp1=0, temp2=0;
	if(NULL == f)
		return 0;
	for(i=0;i<m_nFeaSize;i++)
	{
		for(j=0;j<255;j++)
		{
			fscanf(f,"%lf %lf",&temp1,&temp2);
			PosHist[i*255+j]=temp1;
			NegHist[i*255+j]=temp2;
		}
	}
	fclose(f);
	return 1;

}
int CBayesClas::loadModelFromRes(const char* strResName)
{
	char pModuleName[256];
	::GetModuleFileName(g_hModule,pModuleName,256);

	LPVOID pvModelData=LoadDataResource(strResName,pModuleName);

	if(pvModelData)
	{
	}
	else
	{
		return 0;
	}

	int i,j;
	double temp1=0, temp2=0;
	char* pStreamBuf=(char*)pvModelData;

	istrstream sin(pStreamBuf);
	for(i=0;i<m_nFeaSize;i++)
	{
		
		for(j=0;j<255;j++)
		{
			sin>>temp1>>temp2;
			PosHist[i*255+j]=temp1;
			NegHist[i*255+j]=temp2;
		}	
		int k=0;
	}
	return 1;
/*
	int dl=0;
	for(i=0;i<m_nFeaSize;i++)
	{
		
		for(j=0;j<255;j++)
		{
		//	fscanf(f,"%lf %lf",&temp1,&temp2);
			sscanf(pStreamBuf,"%lf %lf%n",&temp1,&temp2,&dl);
			PosHist[i*255+j]=temp1;
			NegHist[i*255+j]=temp2;
			pStreamBuf+=dl;
		}	
	}

	return 1;
*/
}
double CBayesClas::CompareTwoFeature(unsigned char * p1,unsigned char * p2)
{
//DWORD time1=GetTickCount();
	double Neg=0,Pos=0;
	int index;
	for(int i=0;i<m_nFeaSize;i++)
	{
		index = i*255+abs(p1[i]-p2[i]);
		Pos+=PosHist[index];
		Neg+=NegHist[index];
	}
	double result;
	if(0!=Neg)
	{
		if(0==Pos)
			result = -1;
		else
		{
			result = log(Pos/Neg);
		}
		
	}
	else
		result = -1;
	if(result > 1)
		result =1;
	if(result <-1)
		result = -1;
//DWORD time2=GetTickCount();
//_DebugInfo("c:/bayestime.txt","time=%d",time2-time1);

	return (result+1.0)/2.0;

}
